﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections;

namespace PMSSys
{
    public class treatmentController
    {
        private DBconnentor dbConnect;

        public DBconnentor DbConnect
        {
            get { return dbConnect; }
            set { dbConnect = value; }
        }

        public bool add(TM tm)
        {
            MySqlCommand query = new MySqlCommand("INSERT INTO treatment_medication(p_id, date_issued, test_type, comments)" +
            "VALUES(@P_ID, @DATE_ISSUED, @TEST_TYPE, @COMMENTS)", dbConnect.getConnection());

            query.Parameters.Add(new MySqlParameter("@P_ID", MySqlDbType.Int32)).Value = tm.PatientID;
            query.Parameters.Add(new MySqlParameter("@DATE_ISSUED", MySqlDbType.DateTime)).Value = tm.TMDate;
            query.Parameters.Add(new MySqlParameter("@TEST_TYPE", MySqlDbType.String)).Value = tm.TestType;
            query.Parameters.Add(new MySqlParameter("@COMMENTS", MySqlDbType.String)).Value = tm.Comments;

            return dbConnect.nonQueryExecute(query);
        }
        public DataTable getDataTable(int p_id)
        {
            string str = "SELECT tplan_id, date_issued as `Date Issued`, test_type as `Type` FROM treatment_medication WHERE p_id = " + p_id.ToString() ;
            return dbConnect.view(str);
        }
        public bool update(TM tm)
        {
            MySqlCommand query = new MySqlCommand(@"UPDATE treatment_medication SET date_issued = @DATE_ISSUED, 
            test_type = @TEST_TYPE, comments = @COMMENTS WHERE p_id = @P_ID AND tplan_id = @TPLAN_ID", dbConnect.getConnection());

            query.Parameters.Add(new MySqlParameter("@P_ID", MySqlDbType.Int32)).Value = tm.PatientID;
            query.Parameters.Add(new MySqlParameter("@TPLAN_ID", MySqlDbType.Int32)).Value = tm.TmID;
            query.Parameters.Add(new MySqlParameter("@DATE_ISSUED", MySqlDbType.DateTime)).Value = tm.TMDate;
            query.Parameters.Add(new MySqlParameter("@TEST_TYPE", MySqlDbType.String)).Value = tm.TestType;
            query.Parameters.Add(new MySqlParameter("@COMMENTS", MySqlDbType.String)).Value = tm.Comments;

            return dbConnect.nonQueryExecute(query);
        }
        public TM objectFiller(TM tObject)
        {
            string str = "SELECT * FROM treatment_medication WHERE tplan_id = " + tObject.TmID.ToString();
            DataTable temp = dbConnect.view(str);
            IEnumerator iter = temp.Rows[0].ItemArray.GetEnumerator();
            iter.MoveNext();
            iter.MoveNext();
            tObject.PatientID = (int)iter.Current;
            iter.MoveNext();
            tObject.TMDate = (DateTime) iter.Current;
            iter.MoveNext();
            tObject.TestType = (string)iter.Current;
            iter.MoveNext();
            tObject.Comments = (string)iter.Current;
            return tObject;
        }
        public bool delete(string tmID)
        {
            string str = "DELETE FROM treatment_medication WHERE tplan_id =" + tmID;
            return dbConnect.delete(str);
        }
    }
}
